using System.Linq;
using Magiq.Tests.TestModel;
using NUnit.Framework;

namespace Magiq.Tests {
    public abstract class BaseMagiqProviderTest<TTestClass> where TTestClass : class, ITestClass<TTestClass>, new()
    {
        protected IMagiqTestConfigurator Configurator { get; private set; }
        protected abstract IMagiqTestConfigurator GetConfigurator();
        

        public IQueryable<TTestClass> TestClassInstances { get; private set; }
        public IQueryable<ReferenceTestClass> ReferenceClassInstances { get; private set; }
        public IQueryable<AnotherTestClass> AnotherTestClassInstances { get; private set; }

        protected virtual void InitModel()
        {
            var testDomain = CreateTestDomain();
            
            Configurator.Persist(testDomain.ReferenceClassInstances);
            Configurator.Persist(testDomain.TestClassInstances);

            Configurator.Reload();
            TestClassInstances = Configurator.GetQueryable<TTestClass>();
            ReferenceClassInstances = Configurator.GetQueryable<ReferenceTestClass>();
            AnotherTestClassInstances = Configurator.GetQueryable<AnotherTestClass>();

        }

        protected virtual ITestDomain<TTestClass> CreateTestDomain()
        {
            return new TestDomain<TTestClass>();
        }

        [TestFixtureSetUp]
        public void FixtureSetUp()
        {
            Configurator = GetConfigurator();
        }

        [SetUp]
        public void SetUp()
        {
            Configurator.SetUp();
            InitModel();
            Initialize();
        }

        protected virtual void Initialize()
        {
        }

        [TearDown]
        public void TearDown()
        {
            Configurator.TearDown();
        }

        protected void CheckPoint()
        {
            Configurator.Reload();
        }
    }
}